<?php

/*------------------------------------------------
*
*   Explay Engine Content Management System.
*   Copyright © 2008 Golovdinov Alexander.
*
*-------------------------------------------------
*
*   Official site: wwww.alex-home.spb.ru/explay.html
*   Contact e-mail: golovdinov@gmail.com
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
------------------------------------------------*/

if (!defined ('EXPLAY')) exit;

if (is_cached ('champions.php', 3600)) $content = file_get_contents ($_SERVER['DOCUMENT_ROOT'].'/include/cache/champions.php');
else {
	$content = build_champions ();
	cache_file ('champions.php', $content);
}

$block = array ();
$block['header'] = $block_header;
$block['name'] = $block_name;
$block['content'] = $content;

Templater::set_var ('block', $block);

($align == 'left')
	? CORE::add_block (Templater::display ('block.tpl'), 'left')
	: CORE::add_block (Templater::display ('block.tpl'), 'right');

function build_champions () {
	global $db, $template;
	
	$return = '';
	
	$get = $db->query ("SELECT user_id, user_name, user_login, a.cnt FROM ".DB_PEREFIX."_users AS u
		LEFT JOIN (SELECT COUNT(art_id) AS cnt, art_author FROM ".DB_PEREFIX."_articles GROUP BY art_author) AS a ON a.art_author = u.user_id
		ORDER BY a.cnt DESC LIMIT 0,15");
		
	while (list ($uid, $uname, $ulogin, $count) = $db->fetch_row ($get)) {
	
		Templater::set_var ('id', $uid);
		Templater::set_var ('name', $uname);
		Templater::set_var ('login', $ulogin);
		Templater::set_var ('count', count_smth ($count, array ('статья', 'статьи', 'статей')));
		
			
		$return .= Templater::display ('block_champions_list.tpl');
	}
	
	return $return;
}
